package com.jason.fingerprint.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.R;
import com.jason.fingerprint.adapter.LauncherAdapter;
import com.jason.fingerprint.beans.UserBean;
import com.jason.fingerprint.beans.ui.AppBean;
import com.jason.fingerprint.utils.LauncherUtils;

import org.kymjs.aframe.ui.activity.BaseActivity;
import org.kymjs.aframe.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/23.
 */
public class LauncherActivity extends BaseActivity {

    private static final String TAG = LauncherActivity.class.getSimpleName();

    private GridView mGridView;
    private LauncherAdapter mAdapter;
    private List<AppBean> mAppBeans = new ArrayList<AppBean>();

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_launcher);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mGridView = (GridView) findViewById(R.id.grid);
        mAppBeans = LauncherUtils.getLauncherApps(this);
        mAdapter = new LauncherAdapter(this,mAppBeans);
        mGridView.setAdapter(mAdapter);
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AppBean appBean = mAppBeans.get(position);
                if (appBean.packageName.equals(LauncherActivity.this.getPackageName())){
                    UserBean bean = AppContext.getInstance().getUserBean();
                    if (bean != null && !StringUtils.isEmail(bean.getLoginName())){
                        Intent intent = new Intent(LauncherActivity.this,HomeActivity.class);
                        intent.putExtra("is_to_loading_activity",false);
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(LauncherActivity.this,LoadingActivity.class);
                        startActivity(intent);
                    }
                }else{
                    Intent intent = new Intent();
                    intent = LauncherActivity.this.getPackageManager().getLaunchIntentForPackage(appBean.packageName);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME
                && event.getAction() == KeyEvent.ACTION_DOWN){
            return true;
        }else if (keyCode == KeyEvent.KEYCODE_MENU
                && event.getAction() == KeyEvent.ACTION_DOWN){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
